<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Groups extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'groups';
	/**
	 * Array that contains the validation rules
	 * @access protected
	 * @var array
	 */
	protected $validation_rules = array(
		array(
			'field' => 'name',
			'label' => 'lang:uploking_country_group_name_label',
			'rules' => 'trim|required|min_length[6]|max_length[200]|callback__check_name'
		),
		array(
			'field' => 'description',
			'label' => 'lang:uploking_country_group_description_label',
			'rules' => 'trim|required|min_length[6]|max_length[255]'
		)
	);
	
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('uploking_country_groups_m');
		$this->lang->load('uploking_admin');
		$this->config->load('uploking');
				
		// Load the validation library along with the rules
		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->validation_rules);
		$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
						->append_metadata(js('stoepje.voucher.js', 'voucher'))
						->append_metadata(css('stoepje.voucher.css', 'voucher'));
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index()
	{
		$this->pyrocache->delete_all('modules_m');
		
		// Create pagination links
		$total_rows = $this->uploking_country_groups_m->count_all();
		$pagination = create_pagination('admin/country/groups/index', $total_rows, NULL, 5);
			
		// Using this data, get the relevant results
		$groups = $this->uploking_country_groups_m->order_by('created_time', 'ASC')->limit($pagination['limit'])->get_all();

		$this->template
			->title($this->module_details['name'], lang('stoepje_prize_list_title'))
			->set('groups', $groups)
			->set('pagination', $pagination)
			->build('admin/groups/index', $this->data);
	}
	
	/**
	 * Create method, creates a new category
	 * @access public
	 * @return void
	 */
	public function create()
	{
		// Validate the data
		if ($this->form_validation->run())
		{
			$id = $this->uploking_country_groups_m->insert(array(
				'name'			=> $this->input->post('name'),
				'description'	=> $this->input->post('description'),
				'created_time'			=> time()
			));
			
			if($id){
				$this->session->set_flashdata('success', sprintf( lang('uploking_country_group_add_success_label'), $this->input->post('name')));
				 redirect('admin/country/groups');
			}else{	
				$this->data->message = lang('uploking_country_group_add_error_label');
			}
		}
		
		// Loop through each validation rule
		foreach ($this->validation_rules as $rule)
		{
			$group->{$rule['field']} = set_value($rule['field']);
		}
		
		$this->template
			->title($this->module_details['name'], lang('uploking_country_group_create_title'))
			->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
			->set('group', $group)
			->build('admin/groups/form', $this->data);
	}
	
	/**
	 * Edit method, edits an existing category
	 * @access public
	 * @param int id The ID of the category to edit
	 * @return void
	 */
	public function edit($id = 0)
	{	
		// Get the category
		$group = $this->uploking_country_groups_m->get($id);
		
		// ID specified?
		$group or redirect('admin/country/groups/index');
		$this->form_validation->set_rules(array_merge($this->validation_rules, array(
			'name' => array(
				'field' => 'name',
				'label' => 'lang:uploking_country_group_name_label',
				'rules' => 'trim|required|min_length[6]|max_length[200]|callback__check_name['.$id.']'
			)
		)));
		// Validate the results
		if ($this->form_validation->run())
		{	
			$result = $this->uploking_country_groups_m->update($id, array(
				'name'			=> $this->input->post('name'),
				'description'	=> $this->input->post('description')
			));
			
			if($result){
				$this->session->set_flashdata('success', sprintf( lang('uploking_country_group_edit_success_label'), $this->input->post('name')));
				 redirect('admin/country/groups');
			}else{	
				$this->data->message = lang('uploking_country_group_edit_error_label');
			}
			
		}
		
		// Loop through each rule
		foreach ($this->validation_rules as $rule)
		{
			if ($this->input->post($rule['field']) !== FALSE)
			{
				$group->{$rule['field']} = $this->input->post($rule['field']);
			}
		}

		$this->template
			->title($this->module_details['name'], sprintf(lang('uploking_country_group_edit_title'), $group->name))
			->append_metadata($this->load->view('fragments/wysiwyg', $this->data, TRUE))
			->set('group', $group)
			->build('admin/groups/form');
	}	

	/**
	 * Delete method, deletes an existing category (obvious isn't it?)
	 * @access public
	 * @param int id The ID of the category to edit
	 * @return void
	 */
	public function delete($id = 0)
	{	
		$id_array = (!empty($id)) ? array($id) : $this->input->post('action_to');
		
		// Delete multiple
		if (!empty($id_array))
		{
			$deleted = 0;
			foreach ($id_array as $id)
			{
				if($group = $this->uploking_country_groups_m->get($id)){
					$this->uploking_country_groups_m->delete($id);
					$deleted++;
				}
			}
			if ( $deleted > 0 )
			{
				$this->session->set_flashdata('success', lang('uploking_country_group_delete_success_label'));
			}
		}		
		else
		{
			$this->session->set_flashdata('error', lang('uploking_country_group_delete_error_label'));
		}
		
		redirect('admin/country/groups/index');
	}
		
	/**
	 * Callback method that checks the title of the category
	 * @access public
	 * @param string title The title to check
	 * @return bool
	 */
	public function _check_name($name = '', $id = 0){
		$this->form_validation->set_message('_check_name', sprintf(lang('uploking_country_group_existed_error_label'), $name));
		if($id){
			$this->uploking_country_groups_m->where('id !=', $id);
		}
		if($this->uploking_country_groups_m->get_by('name',$name)){
			return false;
		}else{
			return true;
		}
	}
	
	public function action()
	{
		switch ($this->input->post('btnAction'))
		{			
			case 'delete':
				$this->delete();
				break;
			default:
				redirect('admin/country/groups');
			break;
		}
	}
}